#!/usr/bin/python3 -B

# Copyright 2018 Josh Pieper, jjp@pobox.com.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import shutil
import subprocess
import sys
import tempfile

SCRIPT_PATH = os.path.basename(__file__)

def find_workspace_dir():
    path = SCRIPT_PATH
    while True:
        maybe_workspace = os.path.join(path, 'WORKSPACE')
        if os.path.exists(maybe_workspace):
            return path

        assert path != ''

        old_path = path
        path = os.path.dirname(path)
        assert path != old_path


WORKSPACE_DIR = find_workspace_dir()

def maybe_copy(maybe_filename, dest):
    if not os.path.exists(maybe_filename):
        return

    with open(maybe_filename, 'rb') as f:
        dest.write(f.read())


def get_workspace_var(var):
    search = re.compile(r'^{}\s*=\s*"(.*)"'.format(var))

    with open(os.path.join(WORKSPACE_DIR, 'WORKSPACE'), 'r') as f:
        for line in f:
            match = re.search(search, line)
            if not match:
                continue

            return match.group(1)

    raise RuntimeError('Must supply a {} in the WORKSPACE'.format(var))


BAZEL_REMOTE_SOURCE = 'https://github.com/bazelbuild/bazel/releases/download'

BAZEL_BIN_CACHE = os.path.expanduser('~/.cache/bazel')
BAZEL_VERSION = get_workspace_var('BAZEL_VERSION')
BAZEL_VERSION_SHA = get_workspace_var('BAZEL_VERSION_SHA')


def download_bazel(dest):
    dist_tempfile = tempfile.NamedTemporaryFile(prefix='bazeldl-', delete=False)

    url = '{}/{}/bazel-{}-linux-x86_64'.format(
        BAZEL_REMOTE_SOURCE, BAZEL_VERSION, BAZEL_VERSION)

    print('Downloading bazel {} from {}'.format(
        BAZEL_VERSION, url),
          file=sys.stderr, flush=True)

    subprocess.check_call(['curl', '-L', url, '-o', dist_tempfile.name])

    actual_sha256 = subprocess.check_output([
        'sha256sum', dist_tempfile.name]).decode('utf-8').split(' ')[0]

    if actual_sha256 != BAZEL_VERSION_SHA.lower():
        raise RuntimeError(
            'bazel sha256 does not match, expected {} got {}'.format(
                BAZEL_VERSION_SHA, actual_sha256))

    subprocess.check_call(["chmod", "+x", dist_tempfile.name])
    shutil.move(dist_tempfile.name, dest)


def main():
    bazel_bin_loc = os.path.join(BAZEL_BIN_CACHE, BAZEL_VERSION)

    if not os.path.exists(bazel_bin_loc):
        os.makedirs(os.path.dirname(bazel_bin_loc), exist_ok=True)
        download_bazel(bazel_bin_loc)

    os.execv(bazel_bin_loc, ['bazel', ] + sys.argv[1:])
    assert False


if __name__ == '__main__':
    main()
